package com.IO流;

import sun.reflect.generics.tree.Tree;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/* 定义学生类
2. 创建TreeSet集合，通过比较器排序进行排序
3. 键盘录入学生数据
4. 创建学生对象，把键盘录入的数据对应赋值给学生对象的成员变量
5. 把学生对象添加到TreeSet集合
6. 创建字符缓冲输出流对象
7. 遍历集合，得到每一个学生对象
8. 把学生对象的数据拼接成指定格式的字符串
9. 调用字符缓冲输出流对象的方法写数据
* */
public class 集合排序学生 {
    public static void main(String[] args) throws IOException {
        //创建集合
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                double num=s2.sum()-s1.sum();
                double num1=num==0 ? s1.getYw()-s2.getYw():num;
                double num2=num1==0?s1.getSx()-s2.getSx():num1;
                double num3=num2==0?s1.getYy()-s2.getYy():num2;
            if(num3>0){
                return 1;
            }else if(num3<0){
                return -1;
            }
            return 0;
            }
        });
        Scanner sc = new Scanner(System.in);
        //键盘录入学生数据
        //创建学生对象接收数据
        //将学生对象添加到集合中去
        for (int i = 0; i < 2; i++) {
            System.out.println("姓名");
            String name = sc.next();
            System.out.println("语文成绩");
            double yw = sc.nextDouble();
            System.out.println("数学成绩");
            double sx = sc.nextDouble();
            System.out.println("英语成绩");
            double yy = sc.nextDouble();
            Student stu = new Student(name, yw, sx, yy);
            ts.add(stu);
        }
        //创建字符缓冲输出流
        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\黑马视频\\file\\student\\student.txt"));
        //遍历集合得到每一个学生对象
        //将学生的数据拼接成字符串
        //将字符串写入到文件中去
        for (Student stu:ts) {
            StringBuilder concat= new StringBuilder("");
            concat.append(stu.getName()).append(",").append(stu.getYw()).append(",").append(stu.getSx()).append(",").append(stu.getYy());
            bw.write(concat.toString());
            bw.newLine();
            bw.flush();
        }

    }
}

class Student {
    private String name;
    private double yw;
    private double sx;
    private double yy;

    public Student() {
    }

    public Student(String name, double yw, double sx, double yy) {
        this.name = name;
        this.yw = yw;
        this.sx = sx;
        this.yy = yy;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getYw() {
        return yw;
    }

    public void setYw(double yw) {
        this.yw = yw;
    }

    public double getSx() {
        return sx;
    }

    public void setSx(double sx) {
        this.sx = sx;
    }

    public double getYy() {
        return yy;
    }

    public void setYy(double yy) {
        this.yy = yy;
    }

    public double sum() {
        return yw + yy + sx;
    }
}
